﻿#pragma once
#include "CStrategyRenderCommon.h"

struct _stStrategyRiverD3DVertex
{
	float x, y, z;
	float u, v, Tangentx, Tangenty;				// 纹理坐标 v 沿河流流动方向 0为左 1为右
												// 相对后一点的偏移
};

struct _stStrategyRiverVerticesInfo
{
	std::set<int>			setAreaIn;
	LPDIRECT3DVERTEXBUFFER9	pVB;			// 顶点缓冲区指针
	LPDIRECT3DINDEXBUFFER9	pIB;			// 索引缓冲区指针
	DWORD					dwVerticesNum;	// 顶点个数
	DWORD					dwIndicesNum;	// 索引个数
};

enum _eRiverSign
{
	RS_NormalHead = 0,		// 河流通常的头
	RS_ForkSignSmaller = 1,	// 分叉标记(越来越小)
	RS_ForkSignBigger = 2,	// 分叉标记(越来越大)
	RS_BodyMin = 3,
	RS_BodyMax = 13,
	RS_HeadSignSame = 14,	// 头标记(宽度一致)
	RS_SignMax = 15,
	RS_Sea = 254,
	RS_Blank = 255,
};

enum _eRiverKind
{
	RK_BodySmaller,
	RK_BodyBigger,
	RK_BodySame,		// RS_HeadSignSame
};

struct _stBodyInfo
{
	_stBodyInfo() = default;
	_stBodyInfo(const _stBodyInfo&) = default;
	D3DXVECTOR2 vec2;
	BYTE info;
};
typedef std::deque<_stBodyInfo> _dequeRiverBody;
typedef std::list<_dequeRiverBody> _listAllH2TRiverBody;

class CStrategyRiverRender
{
public:
	CStrategyRiverRender();
	~CStrategyRiverRender();

	HRESULT OnCreateDevice(IDirect3DDevice9* pd3dDevice);
	HRESULT OnResetDevice(IDirect3DDevice9* pd3dDevice);
	void    OnFrameMove(const D3DXMATRIXA16* pWorldViewProject, const D3DXVECTOR4* vCamLookAtDir, double fTime);
	void    OnFrameRender(IDirect3DDevice9* pd3dDevice);
	void    OnLostDevice();
	void    OnDestroyDevice();

private:
	bool	PickRiverHead();
	bool	TraverseRiver(D3DXVECTOR2 ptCenter, D3DXVECTOR2 ptStart, _eRiverKind sign, _dequeRiverBody& dequeRiverBody);
	bool	IsRiverBody(const D3DXVECTOR2& pt);
	bool	IsRiverBody(_eRiverSign e);
	float	GetRiverHeight(float x, float y);
	float	GetRiverWidth(const _dequeRiverBody& deque, int index, bool bSameWidth);

	HRESULT CreateMeshWhenResetDevice(IDirect3DDevice9* pd3dDevice);	// 创建模型
	HRESULT BuildMesh(IDirect3DDevice9* pd3dDevice);

private:
	LPDIRECT3DVERTEXDECLARATION9	m_pDecl;	// 顶点声明接口
	LPD3DXEFFECT					m_pEffect;	// 效果接口

	_listAllH2TRiverBody			m_listAllH2TRiverBody;
	std::list<_stStrategyRiverVerticesInfo>		m_listRiverVerticInfo;

	std::unique_ptr<BYTE[]> m_pRiverBYTEBuf;
};